const responseCode = require("../../config/httpCode");
const router = require("koa-router")({
  prefix: "/auth/upload"
});
const multer = require("koa-multer");
const path = require("path");
const dest_path = path.resolve(__dirname, "../../../static/uploads/");
const storage = multer.diskStorage({
  destination: function(req, file, cb) {
    cb(null, dest_path);
  },
  filename: function(req, file, cb) {
    var fileFormat = file.originalname.split("."); //以点分割成数组，数组的最后一项就是后缀名
    cb(null, Date.now() + "." + fileFormat[fileFormat.length - 1]);
  }
});

const upload = multer({ storage: storage });

router.post("/", upload.single("img"), async ctx => {
  ctx.body = {
    code: 200,
    data: "/uploads/" + ctx.req.file.filename //返回文件名
  };
});
module.exports = router;
